package com.yequan.jms.p2p.queue;


import org.apache.activemq.ActiveMQConnectionFactory;

import javax.jms.*;
import java.util.Random;

public class Producer {

    public static void main(String[] args) {
        int i=0;
        Random random = new Random();
        int randomSecond = random.nextInt(3000) + 1;

        ActiveMQConnectionFactory mqConnectionFactory=null;
        Connection connection=null;
        try {
            //创建连接工厂
            mqConnectionFactory=new ActiveMQConnectionFactory("admin","admin","tcp://192.168.157.141:61616");
            //创建连接
            connection = mqConnectionFactory.createConnection();
            //构建连接
            connection.start();

            //创建会话
            Session session = connection.createSession(true, Session.AUTO_ACKNOWLEDGE);
            //以队列的模式创建
            Queue queue = session.createQueue("test_queue");
            //为消息队列创建消息生产者
            MessageProducer producer = session.createProducer(queue);

            while(true){
                i++;
                TextMessage textMessage = session.createTextMessage();
                textMessage.setText("发送消息--->>>"+i);
                //将消息发送到队列中
                producer.send(textMessage);
                //提交事务
                session.commit();
                //暂停randomSecond毫秒
                Thread.sleep(randomSecond);
            }
        } catch (JMSException e) {
            e.printStackTrace();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

}
